//
//  ZZTaskViewCell.swift
//  QMXK
//
//  Created by jenkin on 2018/12/10.
//  Copyright © 2018年 jenkin. All rights reserved.
//

import UIKit

class ZZTaskViewCell: UIView {
    
    private var model:ZZTaskModel?
    let myLayer:CAShapeLayer! = CAShapeLayer.init()
    var taskModel:ZZTaskModel?{
        get{
            return self.model
        }
        set{
            self.model = newValue
            //self.myLayer.path = self.getPath()
            self.setNeedsDisplay()
        }
    }
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        if self.taskModel == nil{
            super.draw(rect)
            return
        }

        let vW:CGFloat = rect.size.width
        let vh:CGFloat = rect.size.height

        let lineLetfX:CGFloat =  2  //(vW - lineW) / 2
        let lineW:CGFloat = vW - lineLetfX * 2
        let disH:CGFloat = self.taskModel!.allMax - self.taskModel!.allMin
        let rate:CGFloat = vh / disH

        var lineTopY:CGFloat = vh - (self.taskModel!.finish - self.taskModel!.allMin) * rate
        var lineH = (self.taskModel!.finish - self.taskModel!.open) * rate
        let minY = vh - (self.taskModel!.min - self.taskModel!.allMin) * rate
        let maxY = vh - (self.taskModel!.max - self.taskModel!.allMin) * rate
        let centerX = vW / 2

        if self.taskModel!.finish >= self.taskModel!.open{
            UIColor.red.setFill()
        }else{
            UIColor.green.setFill()
            lineH = -lineH
            lineTopY = vh - (self.taskModel!.open - self.taskModel!.allMin) * rate
        }

        let rectBeth:UIBezierPath = UIBezierPath.init(rect: CGRect.init(x: lineLetfX, y: lineTopY, width: lineW, height: lineH))
        print("detail ==", self.taskModel?.toJSONString())
        print("x = ",lineLetfX, "y = ",lineTopY, " w= ",lineW, "h = ",lineH, "allmin = ",self.taskModel?.allMin, "allmax = ", self.taskModel?.allMax)
        rectBeth.lineWidth = 1
        if self.taskModel!.finish >= self.taskModel!.open{
            UIColor.red.setFill()
            UIColor.red.setStroke()
        }else{
            UIColor.green.setFill()
            UIColor.green.setStroke()
        }
        rectBeth.fill()
        rectBeth.move(to: CGPoint.init(x: centerX, y: minY))
        rectBeth.addLine(to: CGPoint.init(x: centerX, y: maxY))
        rectBeth.stroke()
    }
    
    func getPath() -> CGPath{
        
        let rect:CGRect = self.bounds
        
        if self.taskModel == nil{
            super.draw(rect)
            return CGPath.init(rect: rect, transform: nil)
        }
        
        let vW:CGFloat = rect.size.width
        let vh:CGFloat = rect.size.height
        
        let lineW:CGFloat = vW / 2
        let disH:CGFloat = self.taskModel!.allMax - self.taskModel!.allMin
        let lineLetfX:CGFloat = (vW - lineW) / 2
        let rate:CGFloat = vh / disH
        
        var lineTopY:CGFloat = vh - (self.taskModel!.finish - self.taskModel!.allMin) * rate
        var lineH = (self.taskModel!.finish - self.taskModel!.open) * rate
        let minY = vh - (self.taskModel!.min - self.taskModel!.allMin) * rate
        let maxY = vh - (self.taskModel!.max - self.taskModel!.allMin) * rate
        let centerX = vW / 2
        
        if self.taskModel!.finish >= self.taskModel!.open{
            UIColor.red.setFill()
        }else{
            UIColor.green.setFill()
            lineH = -lineH
            lineTopY = vh - (self.taskModel!.open - self.taskModel!.allMin) * rate
        }
        
        let rectBeth:UIBezierPath = UIBezierPath.init(rect: CGRect.init(x: lineLetfX, y: lineTopY, width: lineW, height: lineH))
        print("detail ==", self.taskModel?.toJSONString())
        print("x = ",lineLetfX, "y = ",lineTopY, " w= ",lineW, "h = ",lineH, "allmin = ",self.taskModel?.allMin, "allmax = ", self.taskModel?.allMax)
        rectBeth.lineWidth = 1
        if self.taskModel!.finish >= self.taskModel!.open{
            UIColor.red.setFill()
            UIColor.red.setStroke()
        }else{
            UIColor.green.setFill()
            UIColor.green.setStroke()
        }
        rectBeth.fill()
        rectBeth.move(to: CGPoint.init(x: centerX, y: minY))
        rectBeth.addLine(to: CGPoint.init(x: centerX, y: maxY))
        rectBeth.stroke()
        return rectBeth.cgPath
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
       // self.layer.addSublayer(self.myLayer)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
